home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / passwd+ / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-12  |  7.5 KB  |  391 lines

  1. /*
  2.  * various utilities, like stream readers, error handlers, etc.
  3.  */
  4. #include "passwd.h"
  5.  
  6. /*
  7.  * convert an integer to a string representing that number
  8.  */
  9. char *tonum(n)
  10. int n;                /* number to be ACSII-ized */
  11. {
  12.     char buf[BUFSIZ];        /* temporary buffer */
  13.  
  14.     /*
  15.      * ASCII-ize the number
  16.      */
  17.     SPRINTF(buf, "%d", n);
  18.  
  19.     /*
  20.      * stuff it somewhere safe and return that location
  21.      */
  22.     return(strsave(buf));
  23. }
  24.  
  25. /*
  26.  * save a string in an allocated location
  27.  */
  28. static char *nulstr = "";    /* NULL string used in emergencies */
  29. char *strsave(s)
  30. char *s;            /* string to be saved */
  31. {
  32.     register char *p;        /* points to new chunk of memory */
  33.  
  34.     /*
  35.      * if given nothing, return empty string
  36.      */
  37.     if (s == CH_NULL)
  38.         return(nulstr);
  39.  
  40.     /*
  41.      * do the allocation;
  42.      * if a problem, return the EMPTY string
  43.      */
  44.     if ((p = malloc((unsigned) (strlen(s)+1))) == CH_NULL){
  45.         if (errno < sys_nerr){
  46.             LOG1(LG_SYSTEM, "strsave: %s", sys_errlist[errno]);
  47.         }
  48.         else
  49.             LOG1(LG_SYSTEM, "strsave: unknown error #%d", errno);
  50.         perror("strsave");
  51.         return(nulstr);
  52.     }
  53.  
  54.     /*
  55.      * copy the string to be saved
  56.      * to the newly-allocated memory
  57.      * and return the location of that memory
  58.      */
  59.     (void) strcpy(p, s);
  60.     return(p);
  61. }
  62.  
  63. /*
  64.  * like atoi() but advances pointer
  65.  */
  66. int matoi(str)
  67. char **str;        /* string to be evaluated */
  68. {
  69.     register int n;            /* number to be returned */
  70.  
  71.     /*
  72.      * loop and shrink letters
  73.      */
  74.     n = 0;
  75.     if (isdigit(**str))
  76.         while(isdigit(**str))
  77.             n = n * 10 + *(*str)++ - '0';
  78.     /*
  79.      * return the number
  80.      */
  81.     return(n);
  82. }
  83.  
  84. /*
  85.  * error exit routine
  86.  */
  87. pwexit(ermsg)
  88. char *ermsg;                /* error message */
  89. {
  90.     sigoff();
  91.     FPRINTF(stderr, "%s: %s\n", progname, ermsg);
  92.     exit(1);
  93. }
  94.  
  95. /*
  96.  * for the string formatting
  97.  */
  98. char *sfmt(ptr, sgn, n1, n2, num, string, length)
  99. register char *ptr;            /* used to advance a pointer */
  100. int sgn;                /* sign (1 if "-") */
  101. register int n1;            /* starting point */
  102. register int n2;            /* ending point */
  103. int num;                /* how to format the string */
  104. char *string;                /* the quantity to be interpolated */
  105. char *length;                /* its length */
  106. {
  107.     register int slen;        /* pctstring length */
  108.  
  109.     /*
  110.      * if the string is empty, skip it
  111.      */
  112.     if (string == CH_NULL)
  113.         return(ptr);
  114.  
  115.     /*
  116.      * just a giant switch statement
  117.      */
  118.     switch(num){
  119.     case F_NUMBER:            /* length */
  120.         /*
  121.          * just copy the sucker
  122.          */
  123.         for(n1 = 0; length[n1]; n1++)
  124.             *ptr++ = length[n1];
  125.         break;
  126.     case F_LOWER:            /* lower case */
  127.         if (sgn){
  128.             /*
  129.              * string is shorter than beginning mark
  130.              */
  131.             if ((slen = strlen(string)) <= n1)
  132.                 return(ptr);
  133.             if (slen <= n2)
  134.                 n2 = slen - 1;
  135.             /*
  136.              * just copy the indicated parts of the string, reversed
  137.              */
  138.             for( ; n2 >= n1; n2--)
  139.                 *ptr++ = lowcase(string[n2]);
  140.         }
  141.         else{
  142.             /*
  143.              * string is shorter than beginning mark
  144.              */
  145.             if ((slen = strlen(string)) <= n1)
  146.                 return(ptr);
  147.             /*
  148.              * just copy the indicated parts of the string
  149.              */
  150.             for( ; n1 <= n2 && string[n1]; n1++)
  151.                 *ptr++ = lowcase(string[n1]);
  152.         }
  153.         break;
  154.     case F_FIRST:            /* first upper case */
  155.         if (sgn){
  156.             /*
  157.              * string is shorter than beginning mark
  158.              */
  159.             if ((slen = strlen(string)) <= n1)
  160.                 return(ptr);
  161.             if (slen <= n2)
  162.                 n2 = slen - 1;
  163.             /*
  164.              * just copy the indicated parts of the string, reversed
  165.              */
  166.             for( ; n2 >= n1; n2--)
  167.                 if (n2 == 0)
  168.                     *ptr++ = upcase(string[n2]);
  169.                 else
  170.                     *ptr++ = string[n2];
  171.         }
  172.         else{
  173.             /*
  174.              * string is shorter than beginning mark
  175.              */
  176.             if ((slen = strlen(string)) <= n1)
  177.                 return(ptr);
  178.             /*
  179.              * just copy the indicated parts of the string
  180.              */
  181.             for( ; n1 <= n2 && string[n1]; n1++)
  182.                 if (n1 == 0)
  183.                     *ptr++ = upcase(string[n1]);
  184.                 else
  185.                     *ptr++ = string[n1];
  186.         }
  187.         break;
  188.     case F_UPPER:            /* upper case */
  189.         if (sgn){
  190.             /*
  191.              * string is shorter than beginning mark
  192.              */
  193.             if ((slen = strlen(string)) <= n1)
  194.                 return(ptr);
  195.             if (slen <= n2)
  196.                 n2 = slen - 1;
  197.             /*
  198.              * just copy the indicated parts of the string, reversed
  199.              */
  200.             for( ; n2 >= n1; n2--)
  201.                 *ptr++ = upcase(string[n2]);
  202.         }
  203.         else{
  204.             /*
  205.              * string is shorter than beginning mark
  206.              */
  207.             if ((slen = strlen(string)) <= n1)
  208.                 return(ptr);
  209.             /*
  210.              * just copy the indicated parts of the string
  211.              */
  212.             for( ; n1 <= n2 && string[n1]; n1++)
  213.                 *ptr++ = upcase(string[n1]);
  214.         }
  215.         break;
  216.     default:            /* as is */
  217.         if (sgn){
  218.             /*
  219.              * string is shorter than beginning mark
  220.              */
  221.             if ((slen = strlen(string)) <= n1)
  222.                 return(ptr);
  223.             if (slen <= n2)
  224.                 n2 = slen - 1;
  225.             /*
  226.              * just copy the indicated parts of the string, reversed
  227.              */
  228.             while(n2 >= n1)
  229.                 *ptr++ = string[n2--];
  230.         }
  231.         else{
  232.             /*
  233.              * string is shorter than beginning mark
  234.              */
  235.             if ((slen = strlen(string)) <= n1)
  236.                 return(ptr);
  237.             /*
  238.              * just copy the indicated parts of the string
  239.              */
  240.             while(n1 <= n2 && string[n1])
  241.                 *ptr++ = string[n1++];
  242.         }
  243.         break;
  244.     }
  245.     /*
  246.      * return the new string position
  247.      */
  248.     return(ptr);
  249. }
  250.  
  251. /*
  252.  * for the number formatting
  253.  */
  254. char *nfmt(ptr, sgn, string, length)
  255. register char *ptr;            /* used to advance a pointer */
  256. int sgn;                /* sign (1 if "-") */
  257. char *string;                /* the quantity to be interpolated */
  258. char *length;                /* its length */
  259. {
  260.     register char *ob;    /* used to copy string */
  261.  
  262.     /*
  263.      * just a giant switch statement
  264.      */
  265.     if (!sgn){
  266.         /*
  267.          * if the string is empty, skip it
  268.          */
  269.         if ((ob = string) == CH_NULL)
  270.             return(ptr);
  271.         /*
  272.          * just copy the sucker
  273.          */
  274.         while(*ob)
  275.             *ptr++ = *ob++;
  276.     }
  277.     else{
  278.         /*
  279.          * if the string is empty, skip it
  280.          */
  281.         if ((ob = length) == CH_NULL)
  282.             return(ptr);
  283.         /*
  284.          * just copy the sucker
  285.          */
  286.         while(*ob)
  287.             *ptr++ = *ob++;
  288.     }
  289.     /*
  290.      * return the new string position
  291.      */
  292.     return(ptr);
  293. }
  294.  
  295. /*
  296.  * read a C string
  297.  * returns position to be read next (just after closing quote)
  298.  */
  299. char *getcstring(from, to, quo)
  300. char *from;            /* where to begin */
  301. char *to;            /* copy to here */
  302. char quo;            /* terminal quote mark */
  303. {
  304.     register int n;        /* number from \nnn escape */
  305.     register int i;        /* counter in a for loop */
  306.  
  307.     /*
  308.      * go until string ends or you hit a NUL
  309.      */
  310.     while(*from && *from != quo){
  311.         /*
  312.          * not an escape -- take anything
  313.          */
  314.         if (*from++ != '\\')
  315.             *to++ = from[-1];
  316.         /*
  317.          * escapes
  318.          * -------
  319.          * \ddd -- octal bit pattern
  320.          */
  321.         else if (isdigit(*from)){
  322.             n = 0;
  323.             for(i = 0; i < 3 && isdigit(*from); i++)
  324.                 n = n * 10 + *from++ - '0';
  325.             *to++ = (char) (n&0xff);
  326.         }
  327.         /*
  328.          * \c --- standard C escape
  329.          */
  330.         else switch(*++from){
  331.         case 'n':    *to++ = '\n';        break;
  332.         case 't':    *to++ = '\t';        break;
  333.         case 'b':    *to++ = '\b';        break;
  334.         case 'r':    *to++ = '\r';        break;
  335.         case 'f':    *to++ = '\f';        break;
  336.         case '\\':    *to++ = '\\';        break;
  337.         default:    *to++ = from[-1];    break;
  338.         }
  339.     }
  340.     *to = '\0';
  341.     return(from);
  342. }
  343.  
  344. /*
  345.  * gets the host name, if possible
  346.  */
  347. findhost(name, siznam)
  348. char *name;            /* where to put the name */
  349. int siznam;            /* number of bytes available for name */
  350. {
  351. #ifdef GETHOST
  352.     return(GETHOST(name, siznam));
  353. #else
  354. #ifdef HOSTNAME
  355.     (void) strncpy(name, HOSTNAME, siznam);
  356. #else
  357.     (void) strncpy(name, "* no host name known *", siznam);
  358. #endif
  359.     return(0);
  360. #endif
  361. }
  362.     
  363. /*
  364.  * gets the domain name, if possible
  365.  */
  366. finddomain(name, siznam)
  367. char *name;            /* where to put the name */
  368. int siznam;            /* number of bytes available for name */
  369. {
  370.     register char *p;    /* used to get domain part of host name */
  371.     char buf[BUFSIZ];    /* buffer for name */
  372.  
  373.     if (findhost(buf, BUFSIZ) < 0){
  374. #ifdef GETDOMAIN
  375.         return(GETDOMAIN(name, siznam));
  376. #else
  377.         return(-1);
  378. #endif
  379.     }
  380.     /*
  381.      * everything after first "." is it
  382.      */
  383.     if ((p = index(buf, '.')) == CH_NULL || *++p == '\0')
  384.         return(-1);
  385.     /*
  386.      * copy it in
  387.      */
  388.     (void) strncpy(name, &p[1], siznam);
  389.     return(0);
  390. }
  391.